package com.itwk.gulimall.product.controller;

import com.itwk.common.utils.PageUtils;
import com.itwk.common.utils.R;
import com.itwk.gulimall.product.entity.PmsCategoryBrandRelationEntity;
import com.itwk.gulimall.product.service.PmsCategoryBrandRelationService;
import com.itwk.gulimall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 品牌分类关联
 *
 * @author wangke
 * @email wangke19970512@163.com
 * @date 2021-01-07 23:03:03
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class PmsCategoryBrandRelationController {
    @Autowired
    private PmsCategoryBrandRelationService pmsCategoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
  //  @RequiresPermissions("product:pmscategorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = pmsCategoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /*
    * 根据品牌id ,查询关联信息
    * */
    @RequestMapping("/catelog/list")
    //  @RequiresPermissions("product:pmscategorybrandrelation:list")
    public R categoryBrandRelation(@RequestParam(name = "brandId") Long brandId){
     List< PmsCategoryBrandRelationEntity> list =  pmsCategoryBrandRelationService.getRelation(brandId);

        return R.ok().put("data", list);
    }

    /*
     * /product/categorybrandrelation/brands/list
     *发布商品时根据分类查询出品牌
     * */
    @GetMapping("/brands/list")
     public R getCategoryBrand(@RequestParam (required = false) Long catId){
      List<BrandVo> brandVos = pmsCategoryBrandRelationService.getCategoryBrand(catId);
         return R.ok().put("data", brandVos);
     }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
  //  @RequiresPermissions("product:pmscategorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		PmsCategoryBrandRelationEntity pmsCategoryBrandRelation = pmsCategoryBrandRelationService.getById(id);

        return R.ok().put("pmsCategoryBrandRelation", pmsCategoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
  //  @RequiresPermissions("product:pmscategorybrandrelation:save")
    public R save(@RequestBody PmsCategoryBrandRelationEntity pmsCategoryBrandRelation){
        //避免关联查询，保存时进行冗余字段品牌名字和分类名字保存
		pmsCategoryBrandRelationService.saveDetail(pmsCategoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("product:pmscategorybrandrelation:update")
    public R update(@RequestBody PmsCategoryBrandRelationEntity pmsCategoryBrandRelation){
		pmsCategoryBrandRelationService.updateById(pmsCategoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:pmscategorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		pmsCategoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
